﻿// 强化学习.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int map[5][6] = { {0,0,-1,0,0,0},{0,-1,-1,-1,0,-1},{-1,0,0,-1,0,0},{0,0,-1,0,0,-1},{0,0,0,0,0,1} };
float memory[5][6] = { 0 };
float learning_rate = 0.1;
void RAND(int x, int y,int randing[2])
{
	
	int rx = rand() % 3- 1;
	int ry = rand() % 3 - 1;
	int  examine = 0;
	while (1-examine)
	{
		cout << "randing" << endl;
		while (((x + rx) < 0) || ((x + rx) > 5) || ((y + ry) < 0) || ((y + ry) > 6) ||(rx == 0 && ry == 0))
		{
			 rx = (rand() % 3)-1;
			 ry = (rand() % 3)-1;
			
			examine =examine;
		};
		while (map[x + rx][y + ry] == -1)
		{
			 rx = (rand() % 3)-1;
			ry = (rand() % 3)-1;
			
			examine =examine-1;
		};
		examine = examine + 1;
	};
	 randing[0] = x + rx;
	 randing[1] = y + ry;
	return;
}
void Q_learing_step(int x, int y)
{
	cout << "Q_learn" << endl;
	float value_back = 0;
	for (int i = -1; i <= 1; i =i+1)
	{
		for (int j = -1; j <= 1; j =j+1)
		{
			int copyx = x + i;
			int copyy = y + j;
			if ((copyx < 5) && (copyx >= 0) && (copyy < 6) && (copyy >= 0))
			{
				if (map[copyx][copyy]>=0)
				{
					value_back += learning_rate * memory[copyx][copyy];
					//cout << memory[copyx][copyy] << "map[copyx][copyy]" << endl;
				};
			};
		};
	};
	cout << x << " " << y << endl;
	cout << value_back << endl;
	memory[x][y] = float(map[x][y]) + value_back;

	cout << "tag1" << endl;
	if (x == 5 && y == 5) { return; };
	int rand[2] = { 0 };
	 RAND(x, y,rand);
	//cout << "pass rand" << endl;
	x = rand[0];
	y = rand[1];
	return  Q_learing_step(x, y);
};
void road(int x, int y, char map[5][6])
{
	int  tag = 0;
	while (tag < 30)
	{
		int tagx = x;
		int tagy = y;
		float value = memory[x][y];
		for (int i = -1; i < 2; i++)
		{
			for (int j = -1; j < 2; j++)
			{
				if ((x + i) >= 0 && (x + i) < 5 && (y + j) >= 0 && (y + j) < 6)
				{
					if (memory[x + i][y + j] > value)
					{
						tagx = x + i;
						tagy = y + j;
						value = memory[tagx][tagy];
						//cout << tagx << " " << tagy << value << endl;
					};
				};
			};
		};
		map[tagx][tagy] = 'o';
		x = tagx;
		y = tagy;
		//cout << tagx << " " << tagy << value << endl;
		tag++;
	};
	return;
}
int main()
{
	int circle = 0;
	cout << "设置学习次数" << endl;
	cin >> circle;
	srand(static_cast<unsigned int>(time(0)));
	for (int i = 0; i < circle; i++)
	{
		int x = rand() % 5;
		int y = rand() % 6;
		if (x != y && map[x][y] != -1)
		{
			Q_learing_step(x, y);
			
		};
	};
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 6; j++)
		{
			cout << memory[i][j] << "     ";
		};
		cout << endl;
	};
	char mapchar[5][6] = { {'o','-','p','-','-','-'},{'-','p','p','p','-','p'},{'p','-','-','p','-','-'},
		{'-','-','p','-','-','p'},{'-','-','-','-','-','T'} };
	road(0, 0, mapchar);
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 6; j++)
		{
			cout << mapchar[i][j] << " ";
		}
		cout << endl;
	};



}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
